// pages/autocar/index.js
var util = require('../../utils/util.js');
const app = getApp()
var page = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    listInfo: [],
    dataSource: [],
    loading: true,
    inputShowed: false,
    inputVal: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page = 1;
  },

  //滚动到底部刷新
  onReachBottom: function () {
    this.getCars();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      listInfo: app.globalData.listInfo
    })
    this.getCars();
    this.initBanner();
  },
  //获取企业信息
  getCars: function () {
    var that = this;
    var limit = 10;
    wx.request({
      url: app.globalData.hostUrl + 'handler/getCarlists',
      data: {
        page: page,
        limit: limit,
        cartype:1
      },
      methods: 'GET',
      header: {
        "Content-Type": "json"
      },
      success: function (res) {
       // console.log(res);
        if (res.data.result) {
          page++
          that.setData({
            dataSource: that.data.dataSource.concat(res.data.result)
          });
          if (res.data.result.length < limit) {
            that.setData({
              loading: false
            })
          }
        }
        else {
          that.setData({
            loading: false
          })
          return false
        }
      }
    })
  },
  //广告图片初始化
  initBanner: function () {
    var that = this;
    var requrl = app.globalData.hostUrl + 'common/banner';
    var imgbanner = wx.getStorageSync('autocarBanner');
    //console.log(imgbanner.length);
    if (!app.expiratTime('autocarBanner') || imgbanner.length == 0) {
      wx.request({
        url: requrl,
        data: { 'location': '301' },
        header: {
          'grant-appid': app.globalData.appid
        },
        success: function (res) {
          if (res.statusCode === 200) {
            //console.log(res);
            wx.setStorageSync('autocarBanner', res.data.result);
            that.setData({
              imgUrls: res.data.result
            })
          }
        }
      })
    }
    else {
      that.setData({
        imgUrls: imgbanner
      })
    }
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  srckeyword: function () {
    var keyword = this.data.inputVal;
    this.setData({
      inputVal: "",
      inputShowed: false
    });
    wx.navigateTo({
      url: '../search/carlist?keyword=' + keyword,
    })
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  gohome: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  yuy: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/thappointment',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myappointment',
      })
    }
  },
  orders: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/trorder',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myorder',
      })
    }
  },
  mys: function () {
    wx.redirectTo({
      url: '../myself/myself',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})